package com.tsvetanv.data.binding.property.editors.custom;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.ServletRequestDataBinder;

import com.tsvetanv.data.binding.model.property.editors.custom.PhoneNumber;
import com.tsvetanv.data.binding.model.property.editors.custom.PhoneNumberCommandBean;
import com.tsvetanv.data.binding.model.property.editors.custom.PhoneNumberEditor;

import junit.framework.TestCase;

public class PhoneNumberCommandBeanBindTest extends TestCase {
	PhoneNumberCommandBean bean;
	ServletRequestDataBinder binder;
	MockHttpServletRequest request;

	@Override
	protected void setUp() throws Exception {
		bean = new PhoneNumberCommandBean();
		binder = new ServletRequestDataBinder(bean, "beanName");
		request = new MockHttpServletRequest();
	}

	public void testPhoneNumberCommandBeanBind() {
		binder.registerCustomEditor(PhoneNumber.class, new PhoneNumberEditor());

		request.addParameter("phone", "(222) 333-4444");
		binder.bind(request);

		PhoneNumber expectedPhone = new PhoneNumber();
		expectedPhone.setAreaCode("222");
		expectedPhone.setPrefix("333");
		expectedPhone.setSuffix("4444");

		assertEquals(expectedPhone.getAreaCode(), bean.getPhone().getAreaCode());
	}
}
